package javasec.contempt.visuals;

import java.awt.Frame;
import java.awt.Panel;
import java.awt.BorderLayout;
import java.awt.Point;
import java.awt.geom.Point2D;

import javasec.contempt.data.SeedConnector;

import javax.swing.JFrame;
import javax.swing.JPanel;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.awt.SWT_AWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.EditorPart;


import prefuse.Constants;
import prefuse.Display;
import prefuse.Visualization;
import prefuse.action.ActionList;
import prefuse.action.RepaintAction;
import prefuse.action.assignment.ColorAction;
import prefuse.action.assignment.DataColorAction;
import prefuse.action.layout.graph.ForceDirectedLayout;
import prefuse.activity.Activity;
import prefuse.controls.DragControl;
import prefuse.controls.PanControl;
import prefuse.controls.ZoomControl;
import prefuse.data.DataTypeException;
import prefuse.data.Graph;
import prefuse.data.Node;
import prefuse.data.Table;
import prefuse.data.io.DataIOException;
import prefuse.data.io.GraphMLReader;
import prefuse.data.io.sql.ConnectionFactory;
import prefuse.data.io.sql.DatabaseDataSource;
import prefuse.render.DefaultRendererFactory;
import prefuse.render.LabelRenderer;
import prefuse.util.ColorLib;
import prefuse.util.GraphLib;
import prefuse.visual.NodeItem;
import prefuse.visual.VisualItem;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

import javax.sql.DataSource;


public class LocalNetworkVisual extends EditorPart
{
	//public static String ID = "javasec.contempt.visuals.layer2editor";
	public static String ID = "javasec.contempt.visuals.layer2editor";
	//private Text entry;
	
	public LocalNetworkVisual()
	{
		// TODO Auto-generated constructor stub
	}

	public void doSave(IProgressMonitor monitor)
	{
		// TODO Auto-generated method stub

	}

	public void doSaveAs()
	{
		// TODO Auto-generated method stub

	}

	public void init(IEditorSite site, IEditorInput input)
			throws PartInitException
	{
		System.out.println("new LocalNetworkVisual init()ed");
		// TODO Auto-generated method stub
		setSite(site);
		setInput(input);
		setPartName("Local Network");
	}

	public boolean isDirty()
	{
		// TODO Auto-generated method stub
		return false;
	}

	public boolean isSaveAsAllowed()
	{
		// TODO Auto-generated method stub
		return false;
	}
	
	public void createPartControl(Composite parent)
	{
		try
		{
			
		Composite top = new Composite(parent, SWT.EMBEDDED); 
		Frame frame = SWT_AWT.new_Frame(top);

		Panel panel;
		panel = new Panel(new BorderLayout())
		{ 
			public void update(java.awt.Graphics g)
			{
				/* Do not erase the background */ 
				paint(g); 
			}
		};

		DatabaseDataSource dataSource=null;
		//dataSource=ConnectionFactory.getDatabaseConnection(SeedConnector.instance.jdbcConn);

		Table nodeTable=dataSource.getData("select * from BLUESEED_MAC_NODES");
		Table edgeTable=dataSource.getData("select * from BLUESEED_MAC_NODES where MAC_PARENT_ID>0");
		
		Graph graph = new Graph(nodeTable,edgeTable,false,"MAC_ID","MAC_ID","MAC_PARENT_ID");

		Visualization vis = new Visualization();
		vis.add("graph",graph);
		vis.setInteractive("graph.edges", null, false);
		
		// prefuse step 3
		LabelRenderer renderer = new LabelRenderer("MAC");
		renderer.setRoundedCorner(8, 8);
		vis.setRendererFactory(new DefaultRendererFactory(renderer));		
		
		// prefuse step 4
		int[] palette = new int[]
		{
			//ColorLib.rgb(255,180,180),
			ColorLib.rgb(190,190,255)
		};
		
        // map nominal data values to colors using our provided palette
        DataColorAction fill = new DataColorAction("graph.nodes", "MAC",Constants.NOMINAL, VisualItem.FILLCOLOR, palette);
        // use black for node text
        ColorAction text = new ColorAction("graph.nodes",VisualItem.TEXTCOLOR, ColorLib.gray(0));
        // use light grey for edges
        ColorAction edges = new ColorAction("graph.edges",VisualItem.STROKECOLOR, ColorLib.gray(200));

        // create an action list containing all color assignments
        ActionList color = new ActionList();
        color.add(fill);
        color.add(text);
        color.add(edges);

        // create an action list with an animated layout
        ActionList layout = new ActionList(Activity.INFINITY);
        layout.add(new ForceDirectedLayout("graph"));
        layout.add(new RepaintAction());

        // add the actions to the visualization
        vis.putAction("color", color);
        vis.putAction("layout", layout);

		// prefuse step 5
		Display display = new Display(vis);
		display.setSize(100,100);
		display.addControlListener(new DragControl());
		display.addControlListener(new PanControl());
		display.addControlListener(new ZoomControl());

		// find the node with highest degree (router?)
		int highest=0;
		int target=0;
		for(int x=0;x<graph.getNodeCount();++x)
		{
			if(graph.getNode(x).getDegree()>highest)
			{
				target=x;
				highest=graph.getDegree(x);
			}
		}
		
		// fix node in the visualization
		//((NodeItem)graph.getNode(highest)).setFixed(true);
		VisualItem gatewayItem=null;
		
		gatewayItem=vis.getVisualItem("graph",graph.getNode(target));
		gatewayItem.setFixed(true);
		

		Point gatewayLocation=new Point();
		
		/*
		gatewayLocation.setLocation
				(frame.getMaximizedBounds().getCenterX(),
				frame.getMaximizedBounds().getCenterY());
		
		display.panTo(gatewayLocation);
		System.out.println(gatewayLocation);
		*/
		
		// zoom to node
		display.panAbs(300,200);
		
		
		// prefuse step 6
		frame.add(panel);
		panel.add(display);
		
		vis.run("color");
		vis.run("layout");

		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		
		System.out.println("that was fun");
	}

	public void setFocus()
	{
		System.out.println("focusing editor");
		// TODO Auto-generated method stub

	}

}
